﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class Menu : System.Web.UI.Page
{
    Datautility du = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            _BindMenu();
        }
    }
    private void _BindMenu()
    {
        string sqlstr = "Select * From Group_Permission where GROUP_ID in (select GROUPID from LOGIN where Userid=" + Session["userid"].ToString() + ")";
        DataTable dtpermission = du.getdata(sqlstr);

        string sqlindstr = "Select * From User_Permission where User_ID=" + Session["userid"].ToString();
        DataTable Dtinvipermission = du.getdata(sqlindstr);

        DataSet Ds = new DataSet();
        Ds.ReadXml(Server.MapPath("~/App_Data/Menu.xml"));
        DataTable DTMain1 = Ds.Tables[0];
        DataTable DTDetails = Ds.Tables[1];
        string strFilterSubMenu = "0";
        string strFilterMainMenu = "1,2";

        string loginsqlstr = "Select * from Login Where UserID="+Session["userid"].ToString();
        DataTable dtlogin = du.getdata(loginsqlstr);
        if (dtlogin.Rows.Count > 0)
        {
            //for the Admin 
            #region Admin
            if (dtlogin.Rows[0]["USERTYPE"].ToString() == "0")
            {
                string strMenu = "<ul>";
                if (Dtinvipermission.Rows.Count > 0)
                {
                    for (int j = 0; j < Dtinvipermission.Rows.Count; j++)
                    {
                        strFilterSubMenu += "," + Dtinvipermission.Rows[j]["PAGE_ID"].ToString();
                    }
                }
                else
                {
                    for (int i = 0; i < DTDetails.Rows.Count; i++)
                    {
                        strFilterSubMenu += "," + DTDetails.Rows[i]["moduleType"].ToString();
                    }
                }

                if (Convert.ToBoolean(Session["IsAccEnabled"].ToString()) == true)
                {
                    strFilterMainMenu += ",4";
                    //strFilterSubMenu += ",1";
                }
                if (Convert.ToBoolean(Session["IsInvcEnabled"].ToString()) == true)
                {
                    strFilterMainMenu += ",5";
                    //strFilterSubMenu += ",3";
                }
                if (Convert.ToBoolean(Session["IsInvrEnabled"].ToString()) == true)
                {
                    strFilterMainMenu += ",3";
                    //strFilterSubMenu += ",2";
                }
                strFilterMainMenu += ",6,7";
                DataView DVmain = DTMain1.DefaultView;
                DVmain.RowFilter = "ID IN(" + strFilterMainMenu.ToString() + ")";
                DataTable DTMain = DVmain.ToTable();
                if (DTMain.Rows.Count > 0)
                {
                    for (int i = 0; i < DTMain.Rows.Count; i++)
                    {
                        HtmlGenericControl dynDiv = new HtmlGenericControl("DIV");
                        string strMenuDivId = "Menu_Div_" + DTMain.Rows[i]["MENU_ID"].ToString();
                        dynDiv.ID = strMenuDivId;
                        dynDiv.Attributes.Add("class", "tabcontent");
                        string strContentText = "<div style=\"margin-top:3px;\">";

                        DataView DVDetails = DTDetails.DefaultView;
                        DVDetails.RowFilter = "MENU_ID='" + DTMain.Rows[i]["MENU_ID"].ToString() + "' AND MODULETYPE IN(" + strFilterSubMenu.ToString() + ")";
                        DVDetails.Sort = "TEXT Asc";
                        DataTable DTTemp = DVDetails.ToTable();
                        if (DTTemp.Rows.Count > 0)
                        {
                            for (int j = 0; j < DTTemp.Rows.Count; j++)
                            {
                                //if (DTTemp.Rows[j]["text"].ToString() != "Purchase Order" && DTTemp.Rows[j]["text"].ToString() != "P. O. Manager Approval" && DTTemp.Rows[j]["text"].ToString() != "P. O. Finance Approval" && DTTemp.Rows[j]["text"].ToString() != "P. O. Admin Approval")
                                //{
                                    if (j != DTTemp.Rows.Count - 1)
                                    {
                                        strContentText += "<a class=\"norm\"  href=\"#\"  onclick=\"return ShowSubMenu('" + strMenuDivId + "','" + DTTemp.Rows[j]["URLADDNEW"].ToString() + "','" + DTTemp.Rows[j]["URLVIEW"].ToString() + "','" + DTTemp.Rows[j]["TEXT"].ToString() + "');\">" + DTTemp.Rows[j]["TEXT"].ToString() + "</a> | ";
                                    }
                                    else
                                    {
                                        strContentText += "<a class=\"norm\" href=\"#\" onclick=\"return ShowSubMenu('" + strMenuDivId + "','" + DTTemp.Rows[j]["URLADDNEW"].ToString() + "','" + DTTemp.Rows[j]["URLVIEW"].ToString() + "','" + DTTemp.Rows[j]["TEXT"].ToString() + "');\">" + DTTemp.Rows[j]["TEXT"].ToString() + "</a>";
                                    }
                                //}
                                //if (DTTemp.Rows[j]["MENU_ID"].ToString() == "2")//for Inventory
                                //{
                                //    //for Purchase Order
                                //    if (DTTemp.Rows[j]["MODULETYPE"].ToString() == "417")
                                //    {
                                //        HypPurchaseOrder.Style["display"] = "block";
                                //    }                                    
                                //    //for Manager Approval
                                //    if (DTTemp.Rows[j]["MODULETYPE"].ToString() == "1028")
                                //    {
                                //        HypManagerApproval.Style["display"] = "block";
                                //    }                                    
                                //    //for Finance Approval
                                //    if (DTTemp.Rows[j]["MODULETYPE"].ToString() == "448")
                                //    {
                                //        HypFinanceApproval.Style["display"] = "block";
                                //    }                                    
                                //    //for Admin Approval
                                //    if (DTTemp.Rows[j]["MODULETYPE"].ToString() == "1026")
                                //    {
                                //        HypAdminApproval.Style["display"] = "block";
                                //    }                                    
                                //}
                            }
                        }
                        strContentText += "</div>";
                        dynDiv.InnerHtml = strContentText;
                        TabContainer.Controls.Add(dynDiv);
                        if (DTTemp.Rows.Count > 0)
                        {
                            if (i == 0)
                            {
                                strMenu += "<li><a href=\"javascript:void(0)\" class=\"defaulttab\" rel=\"" + dynDiv.ID.ToString() + "\"><span>" + DTMain.Rows[i]["TEXT"].ToString() + "</span></a></li>";
                            }
                            else
                            {
                                strMenu += "<li><a href=\"javascript:void(0)\" rel=\"" + dynDiv.ID.ToString() + "\"><span>" + DTMain.Rows[i]["TEXT"].ToString() + "</span></a></li>";
                            }
                        }
                    }
                }
                strMenu += "</ul>";
                ddtabs.InnerHtml = strMenu;
            }
            #endregion
            else
            {
                string active = dtlogin.Rows[0]["IsGroupActive"].ToString();
                #region GroupIsFalse
                if (active == "False")
                {
                    string strMenu = "<ul>";
                    if (Dtinvipermission.Rows.Count > 0)
                    {
                        for (int j = 0; j < Dtinvipermission.Rows.Count; j++)
                        {
                            strFilterSubMenu += "," + Dtinvipermission.Rows[j]["PAGE_ID"].ToString();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < DTDetails.Rows.Count; i++)
                        {
                            strFilterSubMenu += "," + DTDetails.Rows[i]["moduleType"].ToString();
                        }
                    }

                    if (Convert.ToBoolean(Session["IsAccEnabled"].ToString()) == true)
                    {
                        strFilterMainMenu += ",4";
                        //strFilterSubMenu += ",1";
                    }
                    if (Convert.ToBoolean(Session["IsInvcEnabled"].ToString()) == true)
                    {
                        strFilterMainMenu += ",5";
                        //strFilterSubMenu += ",3";
                    }
                    if (Convert.ToBoolean(Session["IsInvrEnabled"].ToString()) == true)
                    {
                        strFilterMainMenu += ",3";
                        //strFilterSubMenu += ",2";
                    }
                    strFilterMainMenu += ",6,7";
                    DataView DVmain = DTMain1.DefaultView;
                    DVmain.RowFilter = "ID IN(" + strFilterMainMenu.ToString() + ")";
                    DataTable DTMain = DVmain.ToTable();

                    if (DTMain.Rows.Count > 0)
                    {
                        for (int i = 0; i < DTMain.Rows.Count; i++)
                        {
                            HtmlGenericControl dynDiv = new HtmlGenericControl("DIV");
                            string strMenuDivId = "Menu_Div_" + DTMain.Rows[i]["MENU_ID"].ToString();
                            dynDiv.ID = strMenuDivId;
                            dynDiv.Attributes.Add("class", "tabcontent");
                            string strContentText = "<div style=\"margin-top:3px;\">";

                            DataView DVDetails = DTDetails.DefaultView;
                            DVDetails.RowFilter = "MENU_ID='" + DTMain.Rows[i]["MENU_ID"].ToString() + "' AND MODULETYPE IN(" + strFilterSubMenu.ToString() + ")";
                            DVDetails.Sort = "TEXT Asc";
                            DataTable DTTemp = DVDetails.ToTable();
                            if (DTTemp.Rows.Count > 0)
                            {
                                for (int j = 0; j < DTTemp.Rows.Count; j++)
                                {
                                    if (j != DTTemp.Rows.Count - 1)
                                    {
                                        strContentText += "<a class=\"norm\"  href=\"#\"  onclick=\"return ShowSubMenu('" + strMenuDivId + "','" + DTTemp.Rows[j]["URLADDNEW"].ToString() + "','" + DTTemp.Rows[j]["URLVIEW"].ToString() + "','" + DTTemp.Rows[j]["TEXT"].ToString() + "');\">" + DTTemp.Rows[j]["TEXT"].ToString() + "</a> | ";
                                    }
                                    else
                                    {
                                        strContentText += "<a class=\"norm\" href=\"#\" onclick=\"return ShowSubMenu('" + strMenuDivId + "','" + DTTemp.Rows[j]["URLADDNEW"].ToString() + "','" + DTTemp.Rows[j]["URLVIEW"].ToString() + "','" + DTTemp.Rows[j]["TEXT"].ToString() + "');\">" + DTTemp.Rows[j]["TEXT"].ToString() + "</a>";
                                    }
                                }
                            }
                            strContentText += "</div>";
                            dynDiv.InnerHtml = strContentText;
                            TabContainer.Controls.Add(dynDiv);
                            if (DTTemp.Rows.Count > 0)
                            {
                                if (i == 0)
                                {
                                    strMenu += "<li><a href=\"javascript:void(0)\" class=\"defaulttab\" rel=\"" + dynDiv.ID.ToString() + "\"><span>" + DTMain.Rows[i]["TEXT"].ToString() + "</span></a></li>";
                                }
                                else
                                {
                                    strMenu += "<li><a href=\"javascript:void(0)\" rel=\"" + dynDiv.ID.ToString() + "\"><span>" + DTMain.Rows[i]["TEXT"].ToString() + "</span></a></li>";
                                }
                            }
                        }
                    }
                    strMenu += "</ul>";
                    ddtabs.InnerHtml = strMenu;
                }
                #endregion
                #region Group Is True
                if (active == "True")
                {
                    string strMenu = "<ul>";
                    if (dtpermission.Rows.Count > 0)
                    {
                        for (int j = 0; j < dtpermission.Rows.Count; j++)
                        {
                            strFilterSubMenu += "," + dtpermission.Rows[j]["MODULE_ID"].ToString();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < DTDetails.Rows.Count; i++)
                        {
                            strFilterSubMenu += "," + DTDetails.Rows[i]["moduleType"].ToString();
                        }
                    }

                    if (Convert.ToBoolean(Session["IsAccEnabled"].ToString()) == true)
                    {
                        strFilterMainMenu += ",4";
                        //strFilterSubMenu += ",1";
                    }
                    if (Convert.ToBoolean(Session["IsInvcEnabled"].ToString()) == true)
                    {
                        strFilterMainMenu += ",5";
                        //strFilterSubMenu += ",3";
                    }
                    if (Convert.ToBoolean(Session["IsInvrEnabled"].ToString()) == true)
                    {
                        strFilterMainMenu += ",3";
                        //strFilterSubMenu += ",2";
                    }
                    strFilterMainMenu += ",6,7";
                    DataView DVmain = DTMain1.DefaultView;
                    DVmain.RowFilter = "ID IN(" + strFilterMainMenu.ToString() + ")";
                    DataTable DTMain = DVmain.ToTable();

                    if (DTMain.Rows.Count > 0)
                    {
                        for (int i = 0; i < DTMain.Rows.Count; i++)
                        {
                            HtmlGenericControl dynDiv = new HtmlGenericControl("DIV");
                            string strMenuDivId = "Menu_Div_" + DTMain.Rows[i]["MENU_ID"].ToString();
                            dynDiv.ID = strMenuDivId;
                            dynDiv.Attributes.Add("class", "tabcontent");
                            string strContentText = "<div style=\"margin-top:3px;\">";

                            DataView DVDetails = DTDetails.DefaultView;
                            DVDetails.RowFilter = "MENU_ID='" + DTMain.Rows[i]["MENU_ID"].ToString() + "' AND MODULETYPE IN(" + strFilterSubMenu.ToString() + ")";
                            DVDetails.Sort = "TEXT Asc";
                            DataTable DTTemp = DVDetails.ToTable();
                            if (DTTemp.Rows.Count > 0)
                            {
                                for (int j = 0; j < DTTemp.Rows.Count; j++)
                                {
                                    if (j != DTTemp.Rows.Count - 1)
                                    {
                                        strContentText += "<a class=\"norm\"  href=\"#\"  onclick=\"return ShowSubMenu('" + strMenuDivId + "','" + DTTemp.Rows[j]["URLADDNEW"].ToString() + "','" + DTTemp.Rows[j]["URLVIEW"].ToString() + "','" + DTTemp.Rows[j]["TEXT"].ToString() + "');\">" + DTTemp.Rows[j]["TEXT"].ToString() + "</a> | ";
                                    }
                                    else
                                    {
                                        strContentText += "<a class=\"norm\" href=\"#\" onclick=\"return ShowSubMenu('" + strMenuDivId + "','" + DTTemp.Rows[j]["URLADDNEW"].ToString() + "','" + DTTemp.Rows[j]["URLVIEW"].ToString() + "','" + DTTemp.Rows[j]["TEXT"].ToString() + "');\">" + DTTemp.Rows[j]["TEXT"].ToString() + "</a>";
                                    }
                                }
                            }
                            strContentText += "</div>";
                            dynDiv.InnerHtml = strContentText;
                            TabContainer.Controls.Add(dynDiv);
                            if (DTTemp.Rows.Count > 0)
                            {
                                if (i == 0)
                                {
                                    strMenu += "<li><a href=\"javascript:void(0)\" class=\"defaulttab\" rel=\"" + dynDiv.ID.ToString() + "\"><span>" + DTMain.Rows[i]["TEXT"].ToString() + "</span></a></li>";
                                }
                                else
                                {
                                    strMenu += "<li><a href=\"javascript:void(0)\" rel=\"" + dynDiv.ID.ToString() + "\"><span>" + DTMain.Rows[i]["TEXT"].ToString() + "</span></a></li>";
                                }
                            }
                        }
                    }
                    strMenu += "</ul>";
                    ddtabs.InnerHtml = strMenu;
                }
                #endregion
            }           
        }        
    }
    //protected void LinkButton1_Click(object sender, EventArgs e)
    //{
    //    tblPurchase_Order.Style["display"] = "block";
    //}
}
